local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
loadstring(exports.dgs:dgsImportFunction())()

local UI = {}

font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 11*px )


UI.txt = {}
    UI.txt['success'] = dxCreateTexture('files/success.png', 'argb', false, 'clamp')
    UI.txt['error'] = dxCreateTexture('files/error.png', 'argb', false, 'clamp')
    UI.txt['info'] = dxCreateTexture('files/info.png', 'argb', false, 'clamp')

UI.notifications = {}

function render()
    for i,v in ipairs(UI.notifications) do 
        local a = interpolateBetween(0, 0, 0, 160, 0, 0, (getTickCount()-v.tick)/500, "InOutQuad")
        local a2 = interpolateBetween(0, 0, 0, 255, 0, 0, (getTickCount()-v.tick)/500, "InOutQuad")
        local x = interpolateBetween(300, 0, 0, 0, 0, 0, (getTickCount()-v.tick)/500, "InOutQuad")
        if v.lineH == -1 and not v.animation then 
            animate(-1, 0, 'Linear', v.time, function(value) v.lineH = value v.animation = true end, function() v.animation = false end)
        end

        if getTickCount()-v.tick >= v.time then
            a = interpolateBetween(160, 0, 0, 0, 0, 0, (getTickCount()-(v.tick+v.time))/500, "InOutQuad")
            a2 = interpolateBetween(255, 0, 0, 0, 0, 0, (getTickCount()-(v.tick+v.time))/500, "InOutQuad")
            x = interpolateBetween(0, 0, 0, 300, 0, 0, (getTickCount()-(v.tick+v.time))/500, "InOutQuad")
        end
        local offset = ((i-1)*110)
        local color = (v.type == 'error' and tocolor(255, 41, 41, a) or v.type == 'success' and tocolor(41, 255, 41, a) or v.type == 'info' and tocolor(65, 98, 179, a))
        
        dxDrawRoundedRectangle((1005*px)+x, 80*py+(offset*py), 350*px, 100*py,10, tocolor(0, 0, 0, a), true)
        dxDrawImage((1015*px)+x, 105*py+(offset*py), 52*px, 52*py, UI.txt[v.type], 0, 0, 0, tocolor(255, 255, 255, 100), true)
        dxDrawText(v.text or 'error', (1085*px)+x, 160*py+((offset*2)*py), 250*px, 100*py, tocolor(255, 255, 255, a2), 1, font, "left", "center", false, true, true,true)
        dxDrawRectangle((1010*px)+x, 170*py+(offset*py), (350*(-v.lineH))*px, 5*py, color, true)
    
    end

    for i, v in ipairs(UI.notifications) do
        if getTickCount()-v.tick >= v.time+450 then table.remove(UI.notifications, i) end
        if #UI.notifications <= 0 then
            removeEventHandler("onClientRender", getRootElement(), render)
            UI.isInterface = false
        end
    end
end


function sendNotification(text, type, time,anoti)
    if not text then return false end 
    if #UI.notifications >= (tonumber(anoti) or 4) then table.remove(UI.notifications, 1) end
    UI.notifications[#UI.notifications+1] = {
        text = text,
        type = type or 'info',
        tick = getTickCount(),
        time = time or 7500,
        lineH = -1
    }
    playSound("files/sound.wav")
    if not UI.isInterface then
        UI.isInterface = true
        addEventHandler("onClientRender", getRootElement(), render)
    end
end

addEvent('cmta_notifications:sendNotification', true)
addEventHandler('cmta_notifications:sendNotification', root, function(text, type, time)
    sendNotification(text, type, time)
end)

function dxDrawRoundedRectangle(x, y, width, height, radius, color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y+radius, width-(radius*2), height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawCircle(x+radius, y+radius, radius, 180, 270, color, color, 16, 1, postGUI)
    dxDrawCircle(x+radius, (y+height)-radius, radius, 90, 180, color, color, 16, 1, postGUI)
    dxDrawCircle((x+width)-radius, (y+height)-radius, radius, 0, 90, color, color, 16, 1, postGUI)
    dxDrawCircle((x+width)-radius, y+radius, radius, 270, 360, color, color, 16, 1, postGUI)
    dxDrawRectangle(x, y+radius, radius, height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y+height-radius, width-(radius*2), radius, color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+width-radius, y+radius, radius, height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y, width-(radius*2), radius, color, postGUI, subPixelPositioning)
end
